package com.lahuan.impl;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lahuan.demo.entity.AccountRmb;
import com.lahuan.demo.entity.FrozeAccountRmb;
import com.lahuan.demo.mapper.AccountRmbMapper;
import com.lahuan.demo.service.AccountRmbService;
import com.lahuan.demo.service.FrozeAccountRmbService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
* <p>
* rmb账户服务实现类
* </p>
* @author lahuan
* @since 2022-01-02
*/

@DubboService(version = "1.0.0", interfaceClass = AccountRmbService.class, timeout = 5000)
public class AccountRmbServiceImpl extends ServiceImpl<AccountRmbMapper, AccountRmb> implements AccountRmbService {
    @Autowired
    FrozeAccountRmbService frozeAccountRmbService;
    @Autowired
    AccountRmbServiceImpl accountRmbService;


    @SentinelResource(value = "froze")
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void froze(Long userId, BigDecimal money, String transNo) {
        //冻结
        AccountRmb accountRmb = accountRmbService.getOne(new LambdaQueryWrapper<AccountRmb>().eq(AccountRmb::getUserId, userId));

        if ( accountRmb == null) {
            throw new RuntimeException("账户不存在");
        }
        if (accountRmb.getMoney().compareTo(money) < 0) {
            throw new RuntimeException("账户余额不足");
        }
        //减钱
        accountRmb.setMoney(accountRmb.getMoney().subtract(money));

        FrozeAccountRmb frozeRmb = new FrozeAccountRmb();
        frozeRmb.setFrozeMoney(money);
        frozeRmb.setUserId(userId);
        frozeRmb.setTransNo(transNo);

        accountRmbService.updateById(accountRmb);
        frozeAccountRmbService.save(frozeRmb);

    }
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void add(Long userId, BigDecimal money, String transNo) {
        AccountRmb accountRmb = accountRmbService.getOne(new LambdaQueryWrapper<AccountRmb>().eq(AccountRmb::getUserId, userId));
        if ( accountRmb == null) {
            throw new RuntimeException("账户不存在");
        }
        accountRmb.setMoney(accountRmb.getMoney().add(money));
        //加钱
        accountRmbService.updateById(accountRmb);
        //删除冻结表
        frozeAccountRmbService.remove(new LambdaQueryWrapper<FrozeAccountRmb>().eq(FrozeAccountRmb::getTransNo, transNo));

    }
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void rollback(Long userId, BigDecimal money, String transNo) {
        FrozeAccountRmb frozeAccountRmb = frozeAccountRmbService.getOne(new LambdaQueryWrapper<FrozeAccountRmb>().eq(FrozeAccountRmb::getTransNo, transNo));
        if (frozeAccountRmb != null) {
            //找到还钱的目标
            AccountRmb accountRmbTarget = accountRmbService.getOne(new LambdaQueryWrapper<AccountRmb>().eq(AccountRmb::getUserId, userId));
            //还钱
            accountRmbTarget.setMoney(accountRmbTarget.getMoney().add(money));
            accountRmbService.updateById(accountRmbTarget);
            //删除冻结表
            frozeAccountRmbService.remove(new LambdaQueryWrapper<FrozeAccountRmb>().eq(FrozeAccountRmb::getTransNo, transNo));
        }

    }
}
